<template>
	<view class="cashierDesk">
		<view class="status_bar">
			<!-- 这里是状态栏 -->
		</view>
		<view class="cashierDesk_title">
			<uni-nav-bar :border="false" left-icon="left" backgroundColor="#8D84FF" color="#fff" title="收银台"
				@clickLeft="back" />
		</view>
		<view class="cashierDesk_box">
			<view class="cashierDesk_box_top">
				<view class="cashierDesk_box_top_title">
					<h3>订单信息</h3>
				</view>
				<view class="cashierDesk_box_top_" v-if="res !== ''">
					订单编号：<span style="color: #666;">{{res.payinfo.ordersn}}</span>
				</view>
				<view class="cashierDesk_box_top_" v-if="res !== ''">
					订单总金额：<span style="color: red;">￥{{res.payinfo.money}}</span>
				</view>
			</view>
			<view class="cashierDesk_box_botom" @click="dialogToggle()">
				<!-- 立即支付 -->
				{{res.credit_pay === 1?'余额支付':'积分支付'}}
			</view>
		</view>


		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="info" cancelText="取消" confirmText="确认" title="温馨提示" content="确认要支付吗?"
				@confirm="payFun" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		order_pay,
		order_pay_complete,
		api_index_order_check
	} from '@/api/home.js'
	export default {
		data() {
			return {
				goto: true,
				openid: '',
				id: '',
				res: '',
				tabpay: 0,
				isPay: false
			};
		},
		onLoad(e) {
			this.id = e.orderid;
			uni.getStorage({
				key: 'openid',
				success: (res) => {
					this.openid = res.data;
					this.order_payFun();
				},
				fail: () => {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				}
			});
		},
		onShow() {
			// let settime = setInterval(() => {
			// 	this.api_index_order_checkFun(settime);
			// }, 2000);
		},
		beforeDestroy() {
			this.goto = false;
			console.log(this.goto);
		},
		methods: {
			dialogToggle(type) {
				this.msgType = type
				this.$refs.alertDialog.open()
			},
			dialogClose() {
				this.$refs.alertDialog.close()
			},
			radioChange(e) {
				this.tabpay = e.detail.value;
			},
			async order_payFun() {
				let up = {
					openid: this.openid,
					id: this.id
				}
				let data = await order_pay(up);
				this.res = data;
				// console.log(this.res.intgral_pay);
			},
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			async api_index_order_checkFun(i) {
				let _this = this;
				if (!this.goto) {
					console.log('关闭');
					clearInterval(i)
				}
				uni.request({
					url: 'http://newappadmin.zhongzihuacai.com/app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=api.index.order_check&app=1',
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					method: 'POST',
					data: {
						order_sn: this.res.payinfo.ordersn
					},
					success(res) {
						console.log(res.data);
						if (res.data.status !== 0) {
							uni.navigateTo({
								url: `/pages/confirm_an_order/paysuccess/paysuccess?id=${_this.id}`
							});
							clearInterval(i)
						}
					}
				});
			},
			async payFun() {
				let up = {
					openid: this.openid,
					id: this.id,
					type: this.res.credit_pay === 1 ? 'credit' : 'shop_credit',
				}
				let data = await order_pay_complete(up);
				if (data.status === 1) {
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/home/home'
						})
					}, 3000)
				}
				console.log(data);
			}
		}
	}
</script>

<style lang="scss">
	.cashierDesk {
		width: 100%;
		height: 100vh;

		.status_bar {
			height: var(--status-bar-height);
			width: 100%;
			background-color: #fff;
		}

		.cashierDesk_title {
			width: 100%;
			height: 88rpx;

		}

		.cashierDesk_box {
			padding: 40rpx;

			.cashierDesk_box_top {
				width: 100%;
				height: 200rpx;
				box-shadow: 0 0 20rpx #ccc;
				border-radius: 20rpx;
				padding: 30rpx;
				background-color: #fff;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.cashierDesk_box_top_ {
					font-size: 24rpx;
				}
			}

			.cashierDesk_box_center {
				width: 100%;
				// height: 300rpx;
				margin-top: 30rpx;
				box-shadow: 0 0 20rpx #ccc;
				border-radius: 20rpx;
				overflow: hidden;
				// padding: 30rpx;
				background-color: #fff;
			}

			.cashierDesk_box_botom {
				width: 100%;
				height: 88rpx;
				background-color: #8D84FF;
				text-align: center;
				line-height: 88rpx;
				color: #fff;
				margin-top: 100rpx;
				border-radius: 44rpx;

			}
		}
	}
</style>